package com.xzh.wms.service;

import com.xzh.wms.dao.RolePermissionDao;
import com.xzh.wms.entity.Permission;
import com.xzh.wms.entity.RolePermission;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RolePermissionService {

    @Autowired
    private RolePermissionDao rolePermissionDao;

    public List<RolePermission> select(RolePermission rolePermission){
        return rolePermissionDao.select(rolePermission);
    }

    public List<Permission> selectPermission(Permission Permission){
        return rolePermissionDao.selectPermission(Permission);
    }

    public Integer insert(RolePermission rolePermission){
        Integer roleId = rolePermission.getRoleId();
        List<Permission> permissions = rolePermission.getPermissions();
        //拿到权限ID
        rolePermission.setPermissionIds(permissions.stream().map(Permission::getId).collect(Collectors.toList()));
        //先删除全部权限

        RolePermissionService rolePermissionService = (RolePermissionService) (AopContext.currentProxy());
        rolePermissionService.addPermisson(rolePermission);
        return 0;
    }
    @Transactional
    public  void  addPermisson(RolePermission rolePermission){
        rolePermissionDao.delete(rolePermission);
        if (rolePermission.getPermissionIds().size() > 0){
            rolePermissionDao.insert(rolePermission);
        }
    }

}
